package array

import (
	"fmt"
	"sort"
)

func IsNStraightHand(hand []int, groupSize int) bool {

	if len(hand)%groupSize != 0 {
		return false
	}

	groupCnt := len(hand) / groupSize

	sort.Ints(hand)
	useHand := make([]bool, len(hand))

	for i := 0; i < groupCnt; i++ {
		for j := 0; j < len(hand); j++ {
			temp := []int{}
			if useHand[j] == true {
				continue
			}
			// 加入第一个
			temp = append(temp, hand[j])
			useHand[j] = true

			// 继续加入后续
			for k := j + 1; k < len(hand) && len(temp) < groupSize; k++ {
				// 用过就跳过这个
				if useHand[k] == true {
					continue
				}
				if hand[k] == temp[len(temp)-1]+1 {
					temp = append(temp, hand[k])
					fmt.Println(temp)
					useHand[k] = true
					if len(temp) == groupSize {
						break
					}
				}
			}

			fmt.Println(temp)
			if len(temp) != groupSize {
				return false
			}
		}
	}

	return true

}
